'use strict';

const Controller = require('egg').Controller;

const md5 = require('md5');

/**
* @controller GlobalAdminController 注释必写，swagger-doc是根据这段注释来生成接口的 ）。
*/
class GlobalAdminController extends Controller {
    /**
    * @summary 管理员登录。
    * @description 管理员登录。
    * @router post /globallogin
    * @request body globaladmin value 传入参数
    */
  async login() {
    const params = this.ctx.request.body;
    // // 查询数据库里的信息
    const result = await this.ctx.service.globalAdmin.login(params);
    if(result == null){
      this.ctx.body = {
        msg: "username is wrong",
        status:1,
      }
    }else if(result.password == md5(params.password)){
      this.ctx.session.admin = {
        id:result.id,
        username:result.username,
        nickname:result.nickname,
        language:result.language
      }
      this.ctx.body = {
        msg:"login successful",
        status:3,
      }
    }else{
      this.ctx.body = {
        msg:"password is wrong",
        status:2,
      }
    }
  }
}

module.exports = GlobalAdminController;
